package com.agger.eurekaclientuserservice.controller;

import com.agger.eurekaclientuserservice.exception.UserException;
import com.agger.eurekaclientuserservice.services.UserService;
import com.agger.eurekacommonapi.enums.CommonExceptionEnums;
import com.agger.eurekacommonapi.vo.PageResult;
import com.agger.eurekacommonapi.vo.ResultData;
import com.agger.eurekacommonapi.vo.UserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @classname: UserController
 * @description:
 * @author chenhx
 * @date 2020-04-22 11:47:52
 */
@RestController
@RequestMapping("/user")
public class UserController{
    Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * @Title: sayHello
     * @Description:
     * @param name
     * @return java.lang.String
     * @author chenhx
     * @date 2020-04-23 10:16:51
     */
    @GetMapping("/hello/{name}")
    public String sayHello(@PathVariable("name") String name){
        log.info("这是日志测试{}，值是{}","sayHello",name);
//        log.error("这是日志测试{}，值是{}","sayHello",name);
//        log.warn("这是日志测试{}，值是{}","sayHello",name);
//        log.debug("这是日志测试{}，值是{}","sayHello",name);
//        log.trace("这是日志测试{}，值是{}","sayHello",name);

        return "hello " + name;
    }

    /**
     * @Title: queryUser
     * @Description:
     * @param
     * @return java.lang.String
     * @author chenhx
     * @date 2020-04-29 10:03:55
     */
    @GetMapping("/queryUser")
    public String queryUser(@RequestParam String name){
        log.info("这是日志测试{}，值是{}","queryUser",name);
        log.error("这是日志测试{}，值是{}","queryUser",name);
        log.warn("这是日志测试{}，值是{}","queryUser",name);
        log.debug("这是日志测试{}，值是{}","queryUser",name);
        log.trace("这是日志测试{}，值是{}","queryUser",name);

        if(name.equals("test")){
            // 测试自定义异常，全局异常处理
            throw new UserException(CommonExceptionEnums.ERROR_COMMON,"此用户为测试用户");
        }else if(name.equals("math")){
            // 测试抛出系统异常
            int a = 12/0;
        }else if(name.equals("null")){
            // 测试抛出空指针异常
            new UserVO().getName().substring(0,1);
        }

        return "query " + name;
    }

    /**
     * @Title: queryUserList
     * @Description: 查询列表分页（使用PageHelper分页插件）
     * @param 
     * @return java.lang.String
     * @author chenhx
     * @date 2020-07-20 09:45:01
     */
    @PostMapping("/queryUserList")
    public ResultData<PageResult> queryUserList(@RequestBody UserVO user){

        PageResult<UserVO> page = userService.selectUserList(user);
        if(page!=null){
            return ResultData.SUCCESS(page);
        }
        return ResultData.FAILED("查询数据为空");
    }

    /**
     * @Title: queryUserAccountList
     * @Description: 查询列表分页（使用PageHelper分页插件）
     * @param
     * @return java.lang.String
     * @author chenhx
     * @date 2020-07-20 09:45:01
     */
    @PostMapping("/queryUserAccountList")
    public ResultData<PageResult> queryUserAccountList(@RequestBody UserVO user){

        PageResult<String> page = userService.selectUserAccount(user);
        if(page!=null){
            return ResultData.SUCCESS(page);
        }
        return ResultData.FAILED("查询数据为空");
    }

    /**
     * @Title: queryUserById
     * @Description:
     * @param
     * @return java.lang.String
     * @author chenhx
     * @date 2020-04-29 10:15:19
     */
    @GetMapping("/queryUserById")
    public String queryUserById(@RequestParam String id){
        return "query id = " + id;
    }


    /**
     * @Title: addUser
     * @Description: 保存用户
     * @param
     * @return com.agger.eurekacommonapi.vo.ResultData
     * @author chenhx
     * @date 2020-04-23 13:17:54
     */
    @PostMapping("/addUser")
    public ResultData<UserVO> addUser(@RequestBody UserVO user) {
        int a = userService.addUser(user);
        if(a>0){
            return ResultData.SUCCESS(user).setMessage("保存成功");
        }
        return ResultData.FAILED("保存失败");
    }


}
